package com.syl.service.impl;

import com.github.pagehelper.PageHelper;
import com.syl.dao.ArticleMapper;
import com.syl.dao.CommentMapper;
import com.syl.dao.StatisticMapper;
import com.syl.model.ResponseData.StaticticsBo;
import com.syl.model.domain.Article;
import com.syl.model.domain.Comment;
import com.syl.model.domain.Statistic;
import com.syl.service.ISiteService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SiteServiceImpl implements ISiteService {

  @Autowired
  private CommentMapper commentMapper;
  @Autowired
  private ArticleMapper articleMapper;
  @Autowired
  private StatisticMapper statisticMapper;

  @Override
  public void updateStatistics(Article article) {
    Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId());
    statistic.setHits(statistic.getHits() + 1);
    statisticMapper.updateArticleHitsWithId(statistic);
  }

  @Override
  public List<Comment> recentComments(int limit) {
    PageHelper.startPage(1, limit > 10 || limit < 1 ? 10 : limit);
    List<Comment> byPage = commentMapper.selectNewComment();
    return byPage;
  }

  @Override
  public List<Article> recentArticles(int limit) {
    PageHelper.startPage(1, limit > 10 || limit < 1 ? 10 : limit);
    List<Article> list = articleMapper.selectArticleWithPage();
    // 封装博文统计数据
    for (int i = 0; i < list.size(); i++) {
      Article article = list.get(i);
      Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId());
      article.setHits(statistic.getHits());
      article.setCommentsNum(statistic.getCommentsNum());
    }
    return list;
  }

  @Override
  public StaticticsBo getStatistics() {
    StaticticsBo staticticsBo = new StaticticsBo();
    Integer articles = articleMapper.countArticle();
    Integer comments = commentMapper.countComment();
    staticticsBo.setArticles(articles);
    staticticsBo.setComments(comments);
    return staticticsBo;
  }
}

